package com.zzyl.common.utils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class IdCardUtils {

    /**
     * 从身份证号码获取出生日期
     */
    public static LocalDateTime getBirthDate(String idCard) {
        if (idCard == null || idCard.length() < 14) {
            throw new IllegalArgumentException("无效的身份证号码");
        }
        String birthStr = idCard.substring(6, 14);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        return LocalDateTime.parse(birthStr, formatter);
    }

    /**
     * 从身份证号码获取年龄
     */
    public static Integer getAge(String idCard) {
        LocalDateTime birthDate = getBirthDate(idCard);
        return LocalDateTime.now().getYear() - birthDate.getYear();
    }

    /**
     * 从身份证号码获取性别（1: 男, 0: 女）
     */
    public static Integer getGender(String idCard) {
        char genderChar = idCard.charAt(16);
        return (genderChar - '0') % 2; // 奇数为男，偶数为女
    }
}
